<?php
	/**
	 +=========================================================
	 * 缓存类
	 * CacheAction.class.php
	 +=========================================================
	 * @copyright © 2011 nickdraw.com All rights reserved.
	 * @author NickDraw(零度温柔) webmaster@206c.net
	 * @license http://www.nickdraw.com/license
	 +=========================================================
	 */
	class CacheAction extends CommonAction {
		
		public function index(){
			$this->display();
		}
		
		public function clear(){
			$d = $_POST;
			$_root_dir = str_replace ( "\\", "/", dirname(dirname(dirname(dirname(__FILE__)))) );
			
			$success = '<h2>清除结果：</h2>';
			if ($d['boards']){
				$_cache_path = $_root_dir . '/boards/caches';
				
				$success .= $this->exaction( $_cache_path, $d['board'] );
				if ( $d['board_runtime'] ){
					$success .= $this->delAppRuntime( $_cache_path );
				}
			}
			
			if ($d['applications']){
				$_cache_path = $_root_dir . '/home/caches';
				$success .= $this->exaction( $_cache_path, $d['app'] );
				if ( $d['app_runtime'] ){
					$success .= $this->delAppRuntime( $_cache_path );
				}
			}
			
			$this->assign( "success", $success );
			$this->display();
		}
		
		public function clearall(){
			$_root_dir = str_replace ( "\\", "/", dirname(dirname(dirname(dirname(__FILE__)))) );
			if ( $this->deleteFile( $_root_dir.'/boards/caches' ) === false || $this->deleteFile( $_root_dir.'/home/caches' ) === false ){
				$this->error('清除失败');
			}else{
				$this->success('清除缓存完成');
			}
		}
		
		private function exaction($path, $array){
			$success = '';
			foreach ($array as $key => $val) {
				if ( $this->deleteFile( $path . '/' . $val ) === false ){
					$success .= '<p>清除缓存 <span style="font-family:Verdana;">»</span> <span style="color:red;">' . $val . '失败!</span></p>'."\r\n";
				}else{
					$success .= '<p>清除缓存 <span style="font-family:Verdana;">»</span> <span style="color:green;">' . $val . '成功!</span></p>'."\r\n";
				}
			}
			return $success;
		}
		
		private function delAppRuntime($path){
			$success = '';
			if ( unlink( $path . '/~runtime.php' ) ){
				$success .= '<p>清除缓存 <span style="font-family:Verdana;">»</span> <span style="color:green;">项目配置缓存文件成功!</span></p>'."\r\n";
			}else{
				$success .= '<p>清除缓存 <span style="font-family:Verdana;">»</span> <span style="color:red;">项目配置缓存文件失败!</span></p>'."\r\n";
			}
			return $success;
		}
		
		private function deleteFile($path){
			$text = '';
			if ( is_dir( $path ) ){
				$handle = opendir( $path );			
				while ( $list = readdir( $handle ) ){
					if ($list == '.' || $list == '..'){
						//do nothing
					}else{
						$list = $path.'/'.$list;
					}
					switch ($list){
						case $list == '.' || $list == '..':
							//echo $list.' this is  special directory ';
							continue;
						case is_file($list):
							if (unlink($list)){
								$text = $text.'<SPAN>delete file</SPAN> '.$list.'';
							}else {
								$text = $text. 'delete file failure';
							}
							break;
						case is_dir($list):
							$text = $text. 'open directory '.$list.'';
							$this->deleteFile($list);
							break;
						default:
							//$text=$text.'default action '.$list.'';
							continue;
					}
				}
			}else{
				$text = $text. $path.' sorry the path is not directory';
			}
			return  $text;
		}
		
	}
	
?>